package com.zuipin.controller;

import java.lang.reflect.InvocationTargetException;
import java.text.ParseException;
import java.util.Map;

import javax.annotation.Resource;

import org.apache.log4j.Logger;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.zuipin.framework.annotation.Valid;
import com.zuipin.framework.annotation.Valids;
import com.zuipin.framework.exception.CustomException;
import com.zuipin.framework.result.Result;
import com.zuipin.service.IOrderService;
import com.zuipin.vo.BackOrderResultVo;
import com.zuipin.vo.BackOrderVo;
import com.zuipin.vo.CalculateInfoVo;
import com.zuipin.vo.CalculateResultVo;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;

/**
 * 
 * @ClassName: BackOrderInfoController 
 * @author wuyicheng
 * @time 2017年6月13日上午10:21:58
 */
@Api(value = "后台订单-wuyicheng", description = "后台订单-wuyicheng")
@Controller
@RequestMapping(value = "/backOrder")
public class BackOrderInfoController {
	protected final Logger	log	= Logger.getLogger(getClass());
	@Resource
	private IOrderService	orderService;
	
	/**
	 * @throws ParseException 
	 * @throws CustomException 
	 * 
	 *
	 * @Title: confimReceipt 
	 * @author: wuyicheng
	 * @date: 2017年6月12日下午5:20:55 
	 * @param orderNo
	 * @return
	 * @return: Result
	 * @throws
	 */
	@ResponseBody
	@RequestMapping(value = "/confimReceipt", method = RequestMethod.POST)
	@ApiOperation(notes = "确认收货" + "</br>返回码" + "</br>0-成功" + "</br>1001-参数异常" + "</br>1004-系统异常" + "</br>4034-会员等级异常" + "</br>4035-会员ID异常" + "</br>4036-系统ID异常"
			+ "</br>4037-订单号不存在" + "</br>4038-已发货的才能订单完成!", value = "确认收货")
	public Result<Map<String, Object>> confimReceipt(@ApiParam(name = "orderNo", value = "订单号", required = true) @RequestParam(required = true) String orderNo,
			@ApiParam(name = "memberId", value = "会员ID", required = true) @RequestParam(required = true) Long memberId,
			@ApiParam(name = "sysId", value = "系统ID", required = true) @RequestParam(required = true) Long sysId,
			@ApiParam(name = "sysUserId", value = "后台操作用户", required = false) @RequestParam(required = false) Long sysUserId) throws CustomException {
		Result<Map<String, Object>> res = new Result<>();
		res = orderService.confimReceipt(memberId, sysId, sysUserId, orderNo, null, res, "用户点击确认收货");
		return res;
	}
	
	/**
	 * @throws InvocationTargetException 
	 * @throws IllegalAccessException 
	 * @throws CustomException 
	 * @throws ParseException 
	 * 提交订单按钮
	 *
	 * @Title: createBackOrder 
	 * @author: wuyicheng
	 * @date: 2017年6月12日下午3:55:42 
	 * @param backPayOrderVO
	 * @return
	 * @return: Result<Map<String,Object>>
	 * @throws
	 */
	
	@RequestMapping(value = "/submitBackOrder", method = RequestMethod.POST)
	@ApiOperation(notes = "提交后台订单-wuyicheng" + "</br>返回码" + "</br>0-成功" + "</br>1001-参数异常" + "</br>1004-系统异常" + "</br>2000-商品信息未找到" + "</br>3000-订单错误" + "</br>2000-商品信息未找到"
			+ "</br>4004-无商品可结算" + "</br>4005-收货地址不存在" + "</br>4009-该红包已失效" + "</br>4010-该红包不满足购物车规则，无法使用" + "</br>4018-该红包不存在" + "</br>4019-EMS不支持货到付款" + "</br>4020-红包规则不存在"
			+ "</br>4033-发票信息不完整" + "</br>4040-该地区无配送服务", value = "提交后台订单-wuyicheng")
	@ResponseBody
	@Valids({ @Valid(name = "backOrderVo.createMemberId", required = true), @Valid(name = "backOrderVo.createId", required = true),
			@Valid(name = "backOrderVo.sysId", required = true), @Valid(name = "backOrderVo.useRedPacketId", required = false),
			@Valid(name = "backOrderVo.payType", required = true, regex = "ONLINE|CASH_DELIVERY"),
			@Valid(name = "backOrderVo.expressDeliveryCode", required = true, regex = "ship_sto|ship_sf|ship_ems"),
			@Valid(name = "backOrderVo.orderRemark", required = false, maxLength = 200), @Valid(name = "backOrderVo.customerRemark", required = false, maxLength = 200),
			@Valid(name = "backOrderVo.receiveAddrId", required = true), @Valid(name = "backOrderVo.device", required = true, regex = "PC|WAP"),
			@Valid(name = "backOrderVo.invoiceType", required = true, regex = "NOT_INVOICE|ELECTRONIC_INVOICE|ORDINARY_INVOICE|VTA_INVOICE"),
			@Valid(name = "backOrderVo.freightSum", required = true), @Valid(name = "backOrderVo.orderDetails.proSku", required = true),
			@Valid(name = "backOrderVo.orderDetails.productNum", required = true), @Valid(name = "backOrderVo.orderDetails.isGivePro", required = true, regex = "0|1"), })
	public Result<BackOrderResultVo> submitBackOrder(@RequestBody @ApiParam(value = "后台订单数据") BackOrderVo backOrderVo)
			throws ParseException, CustomException, IllegalAccessException, InvocationTargetException {
		Result<BackOrderResultVo> res = orderService.submitBackOrder(backOrderVo);
		return res;
	}
	
	/**
	 * @throws CustomException 
	 * 修改订单商品
	 *
	 * @Title: toSettleByBack 
	 * @author: wuyicheng
	 * @date: 2017年6月12日下午5:23:30 
	 * @param calculateInfoVo
	 * @return
	 * @return: Result<CalculateResultVo>
	 * @throws
	 */
	@RequestMapping(value = "/toSettleByBack", method = RequestMethod.POST)
	@ApiOperation(notes = "结算订单-wuyicheng" + "</br>返回码" + "</br>0-成功" + "</br>1001-参数异常" + "</br>1004-系统异常" + "</br>2000-商品信息未找到" + "</br>2000-商品信息未找到" + "</br>4004-无商品可结算"
			+ "</br>4005-收货地址不存在" + "</br>4009-该红包已失效" + "</br>4010-该红包不满足购物车规则，无法使用" + "</br>4018-该红包不存在" + "</br>4019-EMS不支持货到付款" + "</br>4020-红包规则不存在"
			+ "</br>4040-该地区无配送服务", value = "结算订单情-wuyicheng")
	@ResponseBody
	@Valids({ @Valid(name = "calculateInfoVo.memberId", required = true), @Valid(name = "calculateInfoVo.sysId", required = true),
			@Valid(name = "calculateInfoVo.useRedPacketId", required = false), @Valid(name = "calculateInfoVo.receiveAddrId", required = true),
			@Valid(name = "calculateInfoVo.device", required = true, regex = "PC|WAP"), @Valid(name = "calculateInfoVo.payType", required = true, regex = "ONLINE|CASH_DELIVERY"),
			@Valid(name = "calculateInfoVo.expressDeliveryCode", required = true, regex = "ship_sto|ship_sf|ship_ems"),
			@Valid(name = "calculateInfoVo.detailVos.proSku", required = true), @Valid(name = "calculateInfoVo.detailVos.productNum", required = true, minValue = 1),
			@Valid(name = "calculateInfoVo.detailVos.isGivePro", required = true, regex = "0|1"), })
	public Result<CalculateResultVo> toSettleByBack(@RequestBody @ApiParam(value = "结算订单") CalculateInfoVo calculateInfoVo) throws CustomException {
		Result<CalculateResultVo> res = orderService.toSettleByBack(calculateInfoVo);
		return res;
	}
	
}
